package com.fanfanwang.ffw.app.asynctask;

import java.util.LinkedHashMap;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.Toast;

import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.bean.SucOrderDoneBean;
import com.fanfanwang.ffw.app.common.Md5Util;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.net.HttpRequest.HttpRequestException;
import com.fanfanwang.ffw.app.net.IssAsyncTask;
import com.fanfanwang.ffw.app.net.IssNetLib;
import com.fanfanwang.ffw.app.net.WebServiceHelper;
import com.fanfanwang.ffw.app.tools.TimeUtils;
import com.fanfanwang.ffw.app.ui.home.SectionDetailAct;

public class OrderDoneAsyncTask extends
		IssAsyncTask<String, String, SucOrderDoneBean> {
	private Activity mActivity;
	private String user_id;
	private String parking_id;
	private String order_starttime;
	private String order_endtime;

	// ($apiusername,$apipassword,$user_id,$parking_id,$order_starttime,$order_endtime)
	// user_id 为用户id parking_id为停车场 order_starttime为预约开始时间 order_endtime为预约结束时间
	public OrderDoneAsyncTask(Activity activity, String user_id,
			String parking_id, String order_starttime, String order_endtime) {
		super(activity);
		this.mActivity = activity;
		this.user_id = user_id;
		this.parking_id = parking_id;
		this.order_starttime = order_starttime;
		this.order_endtime = order_endtime;
	}

	@Override
	protected SucOrderDoneBean doInBackground(String... params) {
		// TODO addAuctionPrice($apiusername,$apipassword,$userid,$pid,$price)
		SucOrderDoneBean bean = null;
		LinkedHashMap<String, Object> paramsMap = new LinkedHashMap<String, Object>();
		paramsMap.put("apiusername", "parking");
		paramsMap.put("apipassword", Md5Util.getMD5To32("parking"));
		paramsMap.put("user_id", user_id);
		paramsMap.put("parking_id", parking_id);
		paramsMap.put("order_starttime", TimeUtils.parseDateToLong(
				order_starttime, "yyyy-MM-dd HH:mm"));
		paramsMap
				.put("order_endtime", TimeUtils.parseDateToLong(order_endtime,
						"yyyy-MM-dd HH:mm"));
		try {
			bean = IssNetLib.getInstance(mActivity).orderDone(
					WebServiceHelper._ORDER_DONE, paramsMap);
		} catch (HttpRequestException e) {
			e.printStackTrace();
			// exception = getResources().getString(R.string.e_net_error);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// exception = getResources().getString(R.string.e_json);
		}
		return bean;
	}

	@Override
	protected void onPostExecute(SucOrderDoneBean result) {
		super.onPostExecute(result);
		if (exception != null) {
			ToastAlone.showToast(mActivity, exception);
			return;
		}
		if (result != null) {
			if (result.status == 1) {
				if (!TextUtils.isEmpty(result.msg)) {
					ToastAlone.showToast(mActivity,
							AppConstant.URLDecoderString(result.msg),
							Toast.LENGTH_SHORT).show();
					SectionDetailAct.iGetParkingInfoable.setOrderSuccess();
				}
			} else if (result.status == 0) {
				if (!TextUtils.isEmpty(result.msg)) {
					ToastAlone.showToast(mActivity,
							AppConstant.URLDecoderString(result.msg),
							Toast.LENGTH_SHORT).show();
				} else {
					ToastAlone.showToast(
							mActivity,
							mActivity.getResources().getString(
									R.string.e_net_error)).show();
				}
			}
		}
	}
}
